<template>

		<b-layout-index>
			<theme-color-bg type="c3">
				<view class="pd-top-30 oval">

					<image mode="widthFix" src="https://res.serkol.cn/LoginImage/1.png"></image>
				</view>
			</theme-color-bg>
			<view class="coverfill br-rd-top-30 ">
				<view class="pd-30">
					<view class="title   font-size-24  font-bold"> {{$t('登录入口')}} </view>
					<view>
						<u--form :borderBottom="false" :model="param" :rules="rules" ref="form1">
							<u-form-item prop="userName">
								<view class="mg-top-20">
									<view class="input br-rd-6">
										<u-cell :border="false">
											<iconfont slot="icon" color="#3c9cff" icon="icon-wode" />
											<!-- 	<input v-model="param.userName" slot="title" placeholder="请输入手机号"
												class="pd-left-10 pd-right-10" /> -->
											<u--input slot="title" class="pd-left-10 pd-right-10 ltr" :placeholder="$t('请输入手机号')"
												v-model="param.userName" border="none"></u--input>
										</u-cell>
									</view>
								</view>
							</u-form-item>
							<u-form-item prop="password">
								<view class="mg-top-10">
									<view class="input br-rd-6">
										<u-cell :border="false">
											<iconfont slot="icon" color="#3c9cff" icon="icon-mima1" />
											<!-- 	<input :value="param.password" slot="title" type="password"
												placeholder="请输入密码" class="pd-left-10 pd-right-10" /> -->
											<u--input type="password" slot="title" class="pd-left-10 pd-right-10 ltr"
												:placeholder="$t('请输入密码')" v-model="param.password" border="none"></u--input>
										</u-cell>
									</view>
								</view>
							</u-form-item>
							<view class="mg-top-10">

								<u-row justify="space-between" customStyle="margin-bottom: 10px">
									<u-col span="3">
										<view class="text-center"><text class="font-size-14">{{$t('忘记密码')}}</text></view>
									</u-col>
									<u-col span="3">
										<view class="text-center"><text class="font-size-14">{{$t('注册')}}</text></view>
									</u-col>
								</u-row>

							</view>


							<view class="mg-top-20">
								<!-- #ifdef H5 -->
								<u-button @click="loginSubmitAccond()" customStyle="border-radius:6px" type="primary"
									:plain="true" :text="$t('登录')">
								</u-button>
								<!-- #endif -->
								<!-- #ifdef MP-WEIXIN  -->
								<u-button @click="loginSubmitAccondMini()" customStyle="border-radius:6px"
									type="primary" :plain="true" :text="$t('登录')">
								</u-button>
								<!-- #endif -->
							</view>

						</u--form>
						<!-- 		<view class="mg-top-10">
							<u-divider text="其他登录方式"></u-divider>
						</view>
						<view class="mg-top-10 text-center">

							<view style="width: 50px;height: 50px; margin: 0 auto;">
								<u-avatar @click="login()" size="50" bgColor="#07c160" font-size="28" icon="weixin-fill"
									shape="circle"></u-avatar>
							</view>

						</view> -->


					</view>
				</view>


			</view>
		</b-layout-index>

</template>

<script>
	import {
		login,
		loginAccount,
		loginAccoundMini,
		loginAccoundMiniOpen
	} from '../../api/oauth.js'
	export default {
		data() {
			return {
				param: {
					userName: "",
					password: ""
				},
				rules: {
					'userName': [{
						type: 'string',
						required: true,
						message: this.$t('请填写姓名'),
						trigger: ['blur', 'change']
					}],
					'password': [{
						type: 'string',
						required: true,
						message: this.$t('请选择男或女'),
						trigger: ['blur', 'change']
					}],
				},
			}
		},
		onLoad: function() {

		},
		methods: {
			login: function(e) {
				var the = this
				console.log("Login")
				//	this.$refs.form1.setRules(this.rules);
				//	let pages = getCurrentPages(); //获取所有页面栈实例列表
				//	let nowPage = pages[pages.length - 1]; //当前页页面实例
				//	let prevPage = pages[pages.length - 2]; //上一页页面实例
				//prevPage.$vm.detailList.receive = item; //修改上一页data里面的地址
				///	console.log("pages", nowPage, prevPage)


				// 获取用户信息
				uni.getUserProfile({
					provider: "weixin",
					lang: 'zh_CN',
					desc: this.$t("确认才能正常试用有些功能"),
					success: function(infoRes) {
						console.log(infoRes)
						var userinfo = infoRes.userInfo;
						the.userInfo = userinfo;
						var param = {
							encryptedData: infoRes.encryptedData,
							iv: infoRes.iv
						};
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								var code = loginRes.code;
								param['code'] = code
								console.log(param);
								var a = login(param)
								a.then(res => {
									uni.removeStorageSync("parentUserId")
									uni.setStorageSync("userInfo", res)
									uni.setStorageSync("Authorization", res.token)
									uni.reLaunch({
										url: "/pages/index/index"
									})
									/* 	uni.navigateBack({
											delta: 1,
											success: function() {
												prevPage.onLoad()
											}
										}) */
									/* 	uni.reLaunch({
											url: "/pages/index/index"
										}) */
								})
							}
						})
					},
					fail: (e) => {
						console.log(e)
					}
				});

			},
			loginSubmitAccond: function() {
				var param = this.param;
				this.$refs.form1.validate().then(res => {
					uni.$u.toast(this.$t('校验通过'))
					console.log(res)
					var a = loginAccount(param.userName, param.password)
					a.then(res => {
						uni.removeStorageSync("parentUserId")
						uni.setStorageSync("userInfo", res)
						uni.setStorageSync("Authorization", res.token)
						uni.reLaunch({
							url: "/pages/index/index"
						})
					})  
				}).catch(errors => {
					console.log(errors)
					uni.$u.toast(this.$t('校验失败'))
				})
			},
			loginSubmitAccondMini: function() {
				var param = this.param;
				var the = this;
				this.$refs.form1.validate().then(res => {
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									var code = loginRes.code;
									param['code'] = code
									console.log(param);
									var a = loginAccoundMiniOpen(param)
									a.then(res => {
										uni.removeStorageSync("parentUserId")
										uni.setStorageSync("userInfo", res)
										uni.setStorageSync("Authorization", res
											.token)
										uni.reLaunch({
											url: "/pages/index/index"
										})
										/* 	uni.navigateBack({
												delta: 1,
												success: function() {
													prevPage.onLoad()
												}
											}) */
										/* 	uni.reLaunch({
												url: "/pages/index/index"
											}) */
									})
								}
							})
					
				}).catch(errors => {
					console.log(errors)
					uni.$u.toast(this.$t('校验失败'))
				})
			}
		}
	}
</script>

<style>
	.tag {
		background-color: #FFFFFF !important;
		height: 100%;
	}

	page {
		background-color: #FFFFFF !important;
	}

	.coverfill {
		background-color: #FFFFFF;
		margin-top: -40px;
		min-height: 200px;
		z-index: 300;
		position: absolute;
		width: 100%;
		top: 300px;
		left: 0;
		height: 67%;
	}

	.br-rd-top-30 {
		border-top-left-radius: 30px;
		border-top-right-radius: 30px;
	}

	.title {
		color: #3c9cff;
	}

	.input {
		background-color: #3c9cff0f;
	}

	.oval {
		text-align: center;
	}
</style>
